home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / dimslib / dims_compute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-26  |  3.5 KB  |  129 lines

  1. /*
  2. ### Compute the fractal dimension (Only phase space) ###
  3. Box algorithm (Swan Kim) implemented by Patrick Worfolk
  4. Requires dims_fn.c and a sorting algorithm, eg. dims_qcksort.c
  5. Note:  We do not really need to store the computed
  6. values in arrays for the current usage, but for future
  7. data manipulation it may be useful.
  8. */
  9.  
  10. #include <stdio.h>
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. void dims_compute(ftp)
  15. FILE *ftp;
  16. {
  17.     int it,dims_len,dims_count(),*dims_n_count, *ivector(),abs(),
  18.         last_was_zero;
  19.     double **data_dims,*dims_n_size,*dims_n_info,*dvector(),**dmatrix();
  20.     FILE *fopen(),*ffp;
  21.     char s[80];
  22.     extern int var_dim,n_stored_data1,dims_scale_max,graph_owner,dims_type_option;
  23.     extern double dims_scale_factor,**data1_x,*all_max,*all_min;
  24.  
  25.     graph_owner = 2;
  26.  
  27.     /* set the length of data for dimensional analysis */
  28.     dims_len = n_stored_data1;
  29.  
  30.  
  31.     /* ERROR CHECKING: VARIABLES IN BOUNDS */
  32.     /* Fatal errors: */
  33.     if (dims_len<1) {
  34.         system_mess_proc(1,
  35.           "(Box algorithm) There is no data.");
  36.         return; }
  37.     if (var_dim<1) {
  38.         system_mess_proc(1,
  39.             "(Box algorithm) Phase space dim error.");
  40.         return; }
  41.     if (dims_scale_max<1) {
  42.         system_mess_proc(1,
  43.             "Need a positive number of scales.");
  44.         return;
  45.         }
  46.     /* Non fatal errors: */
  47.     if (dims_scale_factor<=1) {
  48.         dims_scale_factor=2;
  49.         system_mess_proc(1,
  50.            "Scaling factor must be > 1.  Defaulted to 2.");
  51.         }
  52.     dims_check_max_scales(&dims_scale_factor,&dims_scale_max);
  53.  
  54.     /* Memory allocation */
  55.     dims_n_count = ivector(1,dims_scale_max);
  56.     dims_n_size = dvector(1,dims_scale_max);
  57.     dims_n_info = dvector(1,dims_scale_max);
  58.     if (!(data_dims = dmatrix(0,dims_len-1,0,var_dim-1))) {
  59.         system_mess_proc(1,"(Box algorithm) Insufficient memory.");
  60.         free_ivector(dims_n_count,1,dims_scale_max);
  61.         free_dvector(dims_n_size,1,dims_scale_max);
  62.         free_dvector(dims_n_info,1,dims_scale_max);
  63.         return;
  64.         }
  65.  
  66.     /* Open the file for data storage for analyze routine */
  67.     sprintf(s,"kaos.dims.tmpdat");
  68.     ffp = fopen(s,"w");
  69.  
  70.     /* Initialize */
  71.     last_was_zero=FALSE;
  72.  
  73.     /* Compute bin sizes  */ 
  74.     dims_n_size[1] = (all_max[0]-all_min[0])/dims_scale_factor;
  75.     for (it=2; it<=dims_scale_max; it++) {
  76.         dims_n_size[it]=dims_n_size[it-1]/dims_scale_factor;
  77.         } 
  78.  
  79.     /* Normalize the data */
  80.     dims_normalize(data1_x,data_dims,dims_len,var_dim,all_max,all_min);
  81.     /* Start loop of scaling, sorting, and counting */
  82.     for (it=1; it <= dims_scale_max; it++) { 
  83.         dims_rescale(data_dims,dims_len,var_dim,dims_scale_factor);
  84.         dims_sort(data_dims,dims_len,var_dim);
  85.         dims_count(data_dims,dims_len,var_dim,dims_n_count+it,
  86.         dims_n_info+it);
  87.  
  88.         /* Test to see if we want fractal or infomation dimension */
  89.         if (dims_type_option==0) {
  90.             fprintf(ftp,"%le %d\n",
  91.               dims_n_size[it],dims_n_count[it]);
  92.             fprintf(ffp,"%le %d\n",
  93.               dims_n_size[it],dims_n_count[it]);
  94.             if (it>2 && 
  95.                    0.99*dims_len < dims_n_count[it] &&
  96.                    0.99*dims_len < dims_n_count[it-1])
  97.                 dims_scale_max=it;
  98.         }
  99.         else if (dims_type_option==2) {
  100.             if (dims_n_info[it]>0) {
  101.                 fprintf(ftp,"%le %le\n",
  102.                     dims_n_size[it],dims_n_info[it]);
  103.                 fprintf(ffp,"%le %le\n",
  104.                     dims_n_size[it],dims_n_info[it]);
  105.             }
  106.             else {
  107.                 if (last_was_zero) { /* dropout */
  108.                     dims_scale_max=it-2;
  109.                 }
  110.                 else last_was_zero=TRUE;
  111.             }
  112.         }
  113.  
  114.     }
  115.     fclose(ffp);
  116.  
  117.  
  118.     /* REMOVE THIS LINE BEFORE INCLUDING IN KAOS */
  119.     /*
  120.     analyze(dims_n_size,dims_n_count,dims_n_info,dims_scale_max,var_dim);
  121.     */
  122.  
  123.  
  124.     free_dvector(dims_n_size,1,dims_scale_max);
  125.     free_dvector(dims_n_info,1,dims_scale_max);
  126.     free_ivector(dims_n_count,1,dims_scale_max);
  127.     free_dmatrix(data_dims,0,dims_len-1,0,var_dim-1);
  128. }
  129.